#include "engine.h"

engine::engine()
{
	quit = false;
	s = main;
	background = NULL;
	screen = NULL;
	//audio = NULL;
}

bool engine::Init()
{
	if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
		return false;

	if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096))
		return false;

	screen = SDL_SetVideoMode(600, 480, 32, SDL_SWSURFACE);

	SDL_WM_SetCaption("Alpha Engine", NULL);

	return true;
}

int engine::getState()
{
	return s;
}

void engine::setState(int i)
{

}

void engine::apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
	SDL_Rect offset;

	offset.x = x;
	offset.y = y;

	SDL_BlitSurface(source, clip, destination, &offset);
}

SDL_Surface* engine::load_image(std::string filename)
{
	SDL_Surface *loadedImage = NULL;
	SDL_Surface *optimizedImage = NULL;

	loadedImage = IMG_Load(filename.c_str());

	if(loadedImage != NULL)
	{
		optimizedImage = SDL_DisplayFormat(loadedImage);

		SDL_FreeSurface(loadedImage);
	}

	if(optimizedImage != NULL)
	{
		Uint32 colorkey = SDL_MapRGB(optimizedImage->format,255,255,255);
		SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorkey);
	}

	return optimizedImage;
}

bool engine::CollisionCheck(SDL_Rect A, SDL_Rect B)
{
	int leftA, leftB;
	int rightA, rightB;
	int topA, topB;
	int bottomA, bottomB;

	leftA = A.x;
	rightA = A.x + A.w;
	topA = A.y;
	bottomA = A.y + A.h;

	leftB = B.x;
	rightB = B.x + B.w;
	topB = B.y;
	bottomB = B.y + B.h;

	if( bottomA <= topB )
	{
		return false;
	}

	if( topA >= bottomB )
	{
		return false;
	}

	if( rightA <= leftB )
	{
		return false;
	}

	if( leftA >= rightB )
	{
		return false;
	}

	return true;
}
